package com.rdc.qingkou.model;

import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.config.URL;
import com.rdc.qingkou.dto.CardDTO;
import com.rdc.qingkou.dto.UserInfoDTO;
import com.rdc.qingkou.dto.UserStatusNumDTO;
import com.rdc.qingkou.model.imodel.IPersonalHomepageModel;
import com.rdc.qingkou.presenter.PersonalHomepagePresenter;
import com.rdc.qingkou.retrofitService.IOtherHomepageService;
import com.rdc.qingkou.retrofitService.IPersonalHomepageService;
import com.rdc.qingkou.utils.MyUtils;
import com.rdc.qingkou.vo.CardVO;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Response;

public class PersonalHomepageModel implements IPersonalHomepageModel {

    private PersonalHomepagePresenter presenter;
    private CompositeDisposable mCompositeDisposable = new CompositeDisposable();

    public PersonalHomepageModel(PersonalHomepagePresenter personalHomepagePresenter) {
        presenter = personalHomepagePresenter;
    }

    @Override
    public void getCards(int userId, int currentPage) {
        MyUtils.getRetrofitProvider(URL.GET_CARD_BY_USER_ID)
                .create(IPersonalHomepageService.class)
                .getCards(userId, currentPage)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<List<CardDTO>>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<List<CardDTO>> cardDTOResponse) {
                        if (cardDTOResponse.code() != HttpURLConnection.HTTP_OK) {
                            presenter.getCardError("个人主页获取卡片失败 error code:" + cardDTOResponse.code());
                        }
                        List<CardVO> list = new ArrayList<>();
                        List<CardDTO> dtoList = cardDTOResponse.body();
                        if (dtoList != null) {
                            for (CardDTO card : dtoList) {
                                list.add(card.transformToCard());
                            }
                        }
                        presenter.getCardSuccess(list);
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.getCardError("个人主页获取卡片失败 error:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getStatusNum(int userId) {
        MyUtils.getRetrofitProvider(URL.GET_USER_STATUS_NUM).create(IPersonalHomepageService.class)
                .getUserStatusNum(userId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<UserStatusNumDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<UserStatusNumDTO> response) {
                        if (response.code() != HttpURLConnection.HTTP_OK) {
                            presenter.getStatusNumError("个人主页获取状态数量失败 error code:" + response.code());
                        } else if (response.body().getResult().equals("success")) {
                            List<Integer> list = response.body().getStatusNum();
                            presenter.getStatusNumSuccess(list.get(0), list.get(1), list.get(2));
                        } else {
                            presenter.getStatusNumError("个人主页获取状态数量失败!");
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.getStatusNumError("个人主页获取状态数量失败 error:" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void getMyUserInfo() {
        MyUtils.getRetrofitProvider(URL.GET_USER_INFO).create(IOtherHomepageService.class)
                .getUserInfo(MyApplication.getUser().getUserId())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<Response<UserInfoDTO>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(Response<UserInfoDTO> response) {
                        if (response.code() != HttpURLConnection.HTTP_OK) {
                            presenter.getMyUserInfoError("获取用户信息失败！error code:" + response.code());
                        } else if (!response.body().getResult().equals("success")) {
                            presenter.getMyUserInfoError("获取用户信息失败！");
                        } else {
                            presenter.getMyUserInfoSuccess(response.body().transform());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        presenter.getMyUserInfoError("获取用户信息失败！" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void clearDisposable() {
        mCompositeDisposable.dispose();
    }

    @Override
    public void addDisposable(Disposable disposable) {
        mCompositeDisposable.add(disposable);
    }
}
